<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div id="root">

    </div>
</body>
<script src="../react@17.js"></script>
<script src="../react-dom@17.js"></script>
<script src="../babel.js"></script>
<script type="text/babel">
    let bus = {
        list:[],
        on(callBack){
            this.list.push(callBack);
        },
        emit(msg){
            this.list.forEach((callBack)=>{
                callBack(msg)
            })
        }
    }
    class App extends React.Component{
        render(){
            return (
                <div>
                    <One/>
                    <Two/>
                </div>
            )
        }
    }
    class One extends React.Component{
        state={
            num:100
        }
        render(){
            return (
                <h1>组件1 ---- <button onClick={()=>{
                     this.state.num++;
                     this.setState({
                        num:this.state.num
                     },()=>{  
                     bus.emit(this.state.num)
                     })
                }}>向另一个组件传值</button></h1>
            )
        }
    }
    class Two extends React.Component{
        state = {
            num:""
        }
        render(){
            // 订阅消息
            bus.on((num)=>{
                console.log(num)
                this.setState({
                    num:num
                })
            })
            return (
                <h1>组件2---{this.state.num}</h1>
            )
        }
    }
    ReactDOM.render(<App/>,document.getElementById("root"))
</script>
</html>